<?php

class plan_manager extends CI_Controller {    
    
    function __construct()
	  {
	   parent::__construct();
	   
	   //User queda disponible en todo
	   // el controlador
	   $this->load->model('recipe','',TRUE);   
	    
	  }
    
    public function getPlan()
	{
            $data['age']=$this->input->post('age');
            $data['gender']=$this->input->post('gender');
            $data['height']=$this->input->post('height');
            $data['weight']=$this->input->post('weight');
            
                $hyp = $this->input->post('hypertense');
              if(!empty($hyp)) {
                  $data['hypertense']=1;
              }else{
                  $data['hypertense']=0;
              }
              
              $lac = $this->input->post('lactose');
              
              if(!empty($lac)) {
                  $data['lactose']=1;
              }else{
                  $data['lactose']=0;
              }
              
              $cel = $this->input->post('celiac');
              if(!empty($cel)) {
                  $data['celiac']=1;
              }else{
                  $data['celiac']=0;
              }
              
              $veg = $this->input->post('vegan');
              if(!empty($veg)) {
                  $data['vegan']=1;
              }else{
                  $data['vegan']=0;
              }
            
            //$data['gastritis']=$this->input->post('gastritis');
            
            $data['days']=$this->input->post('days');
                        
           if (!$data['hypertense']){ 
                $data['breakfast'] = $this->recipe->getBreakfast($data['lactose'], $data['celiac'], $data['vegan']);
                $data['lunch'] = $this->recipe->getLunch($data['lactose'], $data['celiac'], $data['vegan']);
                $data['dinner'] = $this->recipe->getDinner($data['lactose'], $data['celiac'], $data['vegan']);
           }else{
               $data['breakfast'] = $this->recipe->getAllRecipes(1,0,0); 
               $data['lunch'] = $this->recipe->getAllRecipes(0,1,0);
               $data['dinner'] = $this->recipe->getAllRecipes(0,0,1);                    
           }  
                   
           $data['calories'] = $this->getCalories($data);
           
            $row['breakfast']=$data['breakfast'];
            $row['lunch']= $data['lunch'];
            $row['dinner']=$data['dinner'];
            
            $plan = array();
           
            $this->distribute(0,0,0,1,$row,$data,$plan);
		
            $data['plan']=$plan;
		$this->load->view('plan_results', $data);
                	                
           }
           
           
           
           
           
           function distribute($breakIdx, $lunchIdx, $dinIdx, $day, $row, $data, $plan){
               
               if ($day < $data['days']){
                   
                   $dailyPlanOk = false;
                   $moreCombinations = true;
                   
                   while(!$dailyPlanOk && $moreCombinations){
                       
                       $plan[$day]['breakfast']=$row['breakfast'][$breakIdx];
                       $plan[$day]['lunch']=$row['lunch'][$lunchIdx];
                       $plan[$day]['dinner']=$row['dinner'][$dinIdx];
                       
                       if (!$data['hypertense']){
                           
                           if ($plan[$day]['breakfast']->calories+$plan[$day]['breakfast']->calories+$plan[$day]['breakfast']->calories <= $data['calories'] ){
                               $dailyPlanOk = true;                               
                           }else{
                               $moreCombinations = $this->nextPlanCombination($breakIdx, $lunchIdx, $dinIdx, $row);                             
                           }                                                                                 
                       }else{
                           //calculate hypertense values                           
                       }                                              
                   }
                   
                   $moreCombinations = $this->nextPlanCombination($breakIdx, $lunchIdx, $dinIdx, $row);
                   if ($moreCombinations){ 
                    $this->distribute($breakIdx, $lunchIdx, $dinIdx, $day+1, $data, $plan);
                   }
               }                               
           }
           
           
           function calculateHypertense(){
               $result = false;
               
               
               
           }
                   
        
           function nextPlanCombination($breakIdx, $lunchIdx, $dinIdx, $row){
               if ($dinIdx + 1 >= count($row['dinner'])){
                   if ($lunchIdx + 1 >= count($row['lunch'])){
                       if ($breakIdx + 1 >= count($row['breakfast'])){
                           return false;                           
                       }else{
                        $dinIdx = 0;                        
                        $lunchIdx = 0;
                        ++$dinIdx;
                        return true;   
                       }
                       
                   }else{
                       $dinIdx = 0;
                       ++$lunchIdx;    
                       return true;
                   }
                   
               }else{
                   ++$dinIdx;
                   return true;
               }
               
           }
                       
        
        function getCalories($data){
            //Male = 10(weight in kg) + 6.25(height in cm) - 5(age in years) + 5
            //Female = 10(weight in kg) + 6.25(height in cm) - 5(age in years) -- 161
            $result = 0;
            if ($data['gender']=='M'){
                $result = 10*$data['weight'] + 6.25*$data['height'] - 5*$data['age'] + 5; 
            }else{
                $result = 10*$data['weight'] + 6.25*$data['height'] - 5*$data['age'] - 161;                 
            }
            return $result;            
        }
        
        
        public function viewDailyPlan($plan){
            $data['breakfast']=$plan['breakfast'];
            $data['lunch']=$plan['lunch'];
            $data['dinner']=$plan['dinner'];
            $this->load->view('daily_results', $data);
            
            
        }
        
    
    
}
?>
